package com.itheima.core.service.impl;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.common.utils.Page;
import com.itheima.core.dao.VegetableDao;
import com.itheima.core.po.Vegetable;
import com.itheima.core.service.VegetableService;
/**
 * 用户Service接口实现类
 */
@Service("vegetableService")
@Transactional
public class VegetableServiceImpl implements VegetableService {
	// 注入VegetableDao
	@Autowired
	private VegetableDao vegetableDao;

	// 用户列表
	public Page<Vegetable> findVegetableList(Integer page, Integer rows, 
			String vegetable_id, String vegetable_name, String describes,String price){
		// 创建客户对象
		Vegetable vegetable = new Vegetable();
		// 判断客户名称
		if(StringUtils.isNotBlank(vegetable_id)){
			vegetable.setVegetable_id(vegetable_id);
		}
		// 判断客户信息来源
		if(StringUtils.isNotBlank(vegetable_name)){
			vegetable.setVegetable_name(vegetable_name);
		}
		if(StringUtils.isNotBlank(describes)){
			vegetable.setDescribes(describes);
		}
		// 判断客户所属行业
		if(StringUtils.isNotBlank(price)){
			vegetable.setPrice(price);
		}
		
		// 当前页
		vegetable.setStart((page-1) * rows) ;
		// 每页数
		vegetable.setRows(rows);
		// 查询客户列表
		List<Vegetable> vegetables = 
                            vegetableDao.selectVegetableList(vegetable);
		// 查询客户列表总记录数
		Integer count = vegetableDao.selectVegetableListCount(vegetable);
		// 创建Page返回对象
		Page<Vegetable> result = new Page<>();
		result.setPage(page);
		result.setRows(vegetables);
		result.setSize(rows);
		result.setTotal(count);
		return result;	
	}
	/**
	 * 创建客户
	 */
	@Override
	public int createVegetable(Vegetable vegetable) {
	    return vegetableDao.createVegetable(vegetable);
	}
	/**
	 * 通过id查询客户
	 */
	@Override
	public Vegetable getVegetableById(String vegetable_id) {	
	    Vegetable vegetable = vegetableDao.getVegetableById(vegetable_id);
	    return vegetable;		
	}
	/**
	 * 更新客户
	 */
	@Override
	public int updateVegetable(Vegetable vegetable) {
	    return vegetableDao.updateVegetable(vegetable);
	}
	/**
	 * 删除客户
	 */
	@Override
	public void deleteVegetable(String vegetable_id) {
	    vegetableDao.deleteVegetable(vegetable_id);	
	}

}
